/*global jQuery setTimeout window event clearTimeout */
/* 
 * jQuery.showMessage.js 1.3 - jQuery plugin
 * Author: Andrew Alba
 * http://showMessage.dingobytes.com/
 *
 * Copyright (c) 2009-2010 Andrew Alba (http://dingobytes.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * Built for jQuery library
 * http://jquery.com
 */
var t;
function closeMessage(delayTime)
{
	t = setTimeout( function(){ 
				jQuery('#showMessage').fadeOut();
		}, delayTime);
}
jQuery(function() {
		jQuery(document).keydown( function(e) {
				var keycode;
				if ( e === null )
				{ // ie
					keycode = event.keyCode;
				}
				else
				{ // mozilla
					keycode = e.which;
				}
				if(keycode == 27)
				{ // close
					jQuery('#showMessage').fadeOut();
					if( typeof(t) != 'undefined' )
					{
						clearTimeout(t);
					}
				}
		});

		jQuery.showMessage = function(options){
				// DEFAULT CONFIGURATION PROPERTIES
				var defaults = {
					thisMessage:		[''],
					className:			'notification',
					position:			'top',
					opacity:			90,
					displayNavigation:	true,
					autoClose:			false,
					delayTime:			5000
				};
				options = jQuery.extend(defaults, options);

			    //first clear all ui=widget
			    if ( jQuery('#showMessage').length )
			    {
					jQuery('#showMessage').remove();
				}
			    // create an messageHolder div
			    var messageHolder = jQuery('<div></div>').css({
						'display': 'none',
						'position': 'fixed',
						'z-index': 101,
						'left': 0,
						'width':'100%',
						'margin':0,
						'filter': 'Alpha(Opacity=' + options.opacity + ')',
						'opacity': options.opacity/100
					})
					.attr('id','showMessage')
					.addClass(options.className);
				if( options.position == 'bottom' )
				{
					jQuery(messageHolder).css('bottom', 0);
				}
				else
				{
					jQuery(messageHolder).css('top', 0);
				}
				if( options.displayNavigation )
				{
					var messageNavigation = jQuery('<span></span>')
							.css({
								'float':'right',
								'padding-right':'1em',
								'font-weight':'bold',
								'font-size':'small'
							})
							.html('Esc Key or ');
							var closeLink = jQuery('<a></a>')
								.attr({
										'href': '',
										'title':'close'
								})
								.css('text-decoration','underline')
								.click( function() {
										jQuery('#showMessage').fadeOut();
										clearTimeout(t);
										return false;
								})
								.text('close');
						jQuery(messageNavigation).append(closeLink);
					jQuery(messageHolder).append(messageNavigation);
				}
				else
				{
					jQuery(window).click(function() {
						if( jQuery('#showMessage').length )
						{
							jQuery('#showMessage').fadeOut();
							jQuery(window).unbind('click');
							if( typeof(t) != 'undefined' )
							{
								clearTimeout(t);
							}
						}
					});
				}
				var stateHolder = jQuery('<div></div>')
						.css({
								'width':'90%',
								'margin':'1em auto',
								'padding':'0.5em'
						});
					    var showmessageul = jQuery('<ul></ul>')
								.css({
										'list-style-image':'none',
										'list-style-position':'outside',
										'list-style-type':'none',
										'font-size':'large',
										'font-weight':'bold',
										'margin-left':0,
										'padding-left':0
							});
					    
						    for (var i = 0; i < options.thisMessage.length; i++)
						    {
						        var showmessageli = jQuery('<li></li>')
										.html(options.thisMessage[i]);
						        jQuery(showmessageul).append(showmessageli);
						    }
						jQuery(stateHolder).append(showmessageul);
					jQuery(messageHolder).append(stateHolder);
				jQuery('body').append(messageHolder);
			jQuery(messageHolder).fadeIn();
			if ( options.autoClose )
			{
				closeMessage(options.delayTime);
			}
		};
});